package com.example.finalproject;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View.OnTouchListener;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class start extends AppCompatActivity implements Runnable {
    private Context context;
    private static final String TAG="start1";
    int time = 30;
    int score = 0;
    int score1 = 0;
    ImageView mouse;
    TextView ScoreShow;
    TextView TimeShow;
    Handler handler;

    public int[][] position = new int[][]{
            {190, 764}, {525, 764}, {910, 781},
            {128, 1270}, {520, 1270}, {910, 1270},
            {128, 1765}, {550, 1785}, {910, 1795}
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        context = this;
        ScoreShow = (TextView) findViewById(R.id.ScoreShow);
        TimeShow = (TextView) findViewById(R.id.TimeShow);
        TimeShow.setText(String.valueOf(time));
        mouse = (ImageView) findViewById(R.id.imageView);

        mouse.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                v.setVisibility(View.INVISIBLE);       //设置地鼠不显示
                score++;
                score1 = 3 * score;
                ScoreShow.setText(String.valueOf(score1));
                Toast.makeText(start.this, "打到地鼠了！",
                        Toast.LENGTH_SHORT).show();
                return false;
            }
        });


        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                int index = 0;
                if (msg.what == 0x101) {
                    index = msg.arg1;     //获取位置索引值
                    TimeShow.setText(String.valueOf(time));
                    mouse.setX(position[index][0]);      //设置X轴位置
                    mouse.setY(position[index][1]);      //设置Y轴位置
                    mouse.setVisibility(View.VISIBLE);   //设置地鼠显示
                }
                super.handleMessage(msg);
            }

        };
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                int index = 0;      //记录地鼠位置的索引值
                while (!Thread.currentThread().isInterrupted()) {
                    index = new Random().nextInt(position.length);     //产生一个二维数组长度以内的随机数
                    Message m = handler.obtainMessage();
                    m.what = 0x101;    //消息标识
                    m.arg1 = index;    //保存地鼠位置的索引值
                    handler.sendMessage(m);      //发送信息
                    if(time==0)
                        break;
                    try {      //抛出异常
                        Thread.sleep(new Random().nextInt(500) + 500);   //线程睡眠
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        t.start();     //开启线程


            Thread t2 = new Thread(new Runnable() {
                @Override
                public void run() {
                    while (!Thread.currentThread().isInterrupted()) {
                        time--;
                        if(time==0)
                        {
                            AlertDialog.Builder builder = new AlertDialog.Builder(context);
                            builder.setTitle("时间到！");
                            builder.setMessage("你的成绩是："+score1);

                            DBManger dbManger = new DBManger(context);
                            HistoryItem historyItem = new HistoryItem();
                            historyItem.setDifficulty("简单");
                            historyItem.setScore(score1);
                            Log.i(TAG,"easyscore="+score1);
                            dbManger.add(historyItem);


                            builder.setPositiveButton("退出", new DialogInterface.OnClickListener(){
                                @Override
                                public void onClick(DialogInterface dialog,int which) {
                                    //处理退出对话框的事项.
                                    Button PositiveButton = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
                                    Intent intent =new Intent(context,MainActivity.class);
                                    startActivityForResult(intent,1);
                                } });
                            Looper.prepare();
                            builder.show();
                            Looper.loop();
                            break;}
                        try {      //抛出异常
                            Thread.sleep(new Random().nextInt(1000) + 1000);   //线程睡眠
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }


            });
            t2.start();     //开启线程


}




    @Override
    public void run() {

    }

    public void btn(View v){
        if(v.getId()==R.id.exit){
            Intent MainActivity =new Intent(this,MainActivity.class);
            startActivityForResult(MainActivity,1); }
    }





}